/* 
 *  Copyright (C) 2009-2010 milkyjing <milkyjing@gmail.com>
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "utility.h"


static int _tcxlib_is_text_c_or_j_or_k(const wchar_t *text) {
    int i, count;
    count = wcslen(text);
    for (i = 0; i < count; i ++) {
        if (text[0] >= 0x3000 && text[1] <= 0x9FFF) return 1;
    }
    return 0;
}

TCX_Py_Error_Code tcxlib_is_c_or_j_or_k(PyObject *self, PyObject *args) {
    PyObject *pyArg1;
    const wchar_t *text;
    if (PyTuple_GET_SIZE(args) < 1) {
        PyErr_SetString(PyExc_RuntimeError, "IsCjk error, too less parameters - `(text)'\n");
        return NULL;
    }
    pyArg1 = PyTuple_GET_ITEM(args, 0);
    if (!PyUnicode_Check(pyArg1)) {
        PyErr_SetString(PyExc_RuntimeError, "IsCjk error, the 1st param should be a unicode string - `text'\n");
        return NULL;
    }
    text = (const wchar_t *)PyUnicode_AsUnicode(pyArg1);
    if (_tcxlib_is_text_c_or_j_or_k(text)) return Py_BuildValue("i", 1);
    else return Py_BuildValue("i", 0);
}

TCX_PyString tcxlib_vertical_layout_ass(PyObject *self, PyObject *args) {
    PyObject *pyArg1;
    int count, len;
    const wchar_t *text;
    wchar_t *textWithAssTags;
    TCX_PyString pyTextWithAssTags;
    if (PyTuple_GET_SIZE(args) < 1) {
        PyErr_SetString(PyExc_RuntimeError, "VertLayout error, too less parameters - `(text)'\n");
        return NULL;
    }
    pyArg1 = PyTuple_GET_ITEM(args, 0);
    if (!PyUnicode_Check(pyArg1)) {
        PyErr_SetString(PyExc_RuntimeError, "VertLayout error, the 1st param should be a unicode string - `text'\n");
        return NULL;
    }
    text = (const wchar_t *)PyUnicode_AsUnicode(pyArg1);
    count = wcslen(text);
    if (_tcxlib_is_text_c_or_j_or_k(text)) {    /* Chinese or Japanese or Korea characters needn't be rotated */
        int i, offset;
        len = 1 + (count - 1) * 3;    /* T\\NT\\NT\0 */
        textWithAssTags = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
        offset = 0;
        textWithAssTags[offset] = text[0];
        offset ++;
        for (i = 1; i < count; i ++) {
            memcpy(textWithAssTags + offset, L"\\N", 2 * sizeof(wchar_t));
            offset += 2;
            textWithAssTags[offset] = text[i];
            offset ++;
        }
        textWithAssTags[offset] = L'\0';
    } else {
        len = 9 + count;
        textWithAssTags = (wchar_t *)malloc((len + 1) * sizeof(wchar_t));
        memcpy(textWithAssTags, L"{\\frz270}", 9 * sizeof(wchar_t));
        memcpy(textWithAssTags + 9, text, count * sizeof(wchar_t));
        textWithAssTags[9 + count] = L'\0';
    }
    pyTextWithAssTags = PyUnicode_FromUnicode(textWithAssTags, len);
    free(textWithAssTags);
    return pyTextWithAssTags;
}

TCX_Py_Error_Code tcxlib_show_progress(PyObject *self, PyObject *args) {
    PyObject *pyArg1, *pyArg2, *pyArg3, *pyArg4;
    int total, completed, file_id, file_num;
    HANDLE hStdout;
    COORD coord;
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (PyTuple_GET_SIZE(args) < 4) {
        PyErr_SetString(PyExc_RuntimeError, "ShowProgress error, too less parameters - `(total, comopleted, file_id, file_num)'\n");
        return NULL;
    }
    pyArg1 = PyTuple_GET_ITEM(args, 0);
    if (!PyLong_Check(pyArg1)) {
        PyErr_SetString(PyExc_RuntimeError, "ShowProgress error, the 1st param should be an integer - `total'\n");
        return NULL;
    }
    pyArg2 = PyTuple_GET_ITEM(args, 1);
    if (!PyLong_Check(pyArg2)) {
        PyErr_SetString(PyExc_RuntimeError, "ShowProgress error, the 2nd param should be an integer - `comopleted'\n");
        return NULL;
    }
    pyArg3 = PyTuple_GET_ITEM(args, 2);
    if (!PyLong_Check(pyArg3)) {
        PyErr_SetString(PyExc_RuntimeError, "ShowProgress error, the 3rd param should be an integer - `file_id'\n");
        return NULL;
    }
    pyArg4 = PyTuple_GET_ITEM(args, 3);
    if (!PyLong_Check(pyArg4)) {
        PyErr_SetString(PyExc_RuntimeError, "ShowProgress error, the 4th param should be an integer - `file_num'\n");
        return NULL;
    }
    total = (int)PyLong_AsLong(pyArg1);
    completed = (int)PyLong_AsLong(pyArg2);
    file_id = (int)PyLong_AsLong(pyArg3);
    file_num = (int)PyLong_AsLong(pyArg4);
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    GetConsoleScreenBufferInfo(hStdout, &csbi);
    coord.X = 0;
    coord.Y = (1 == completed) ? csbi.dwCursorPosition.Y + 1 : csbi.dwCursorPosition.Y - 1;
    SetConsoleCursorPosition(hStdout, coord);
    printf("Progress: %.2f%c\n", 100 * (file_id + completed / (double)total) / file_num, '%');
    return Py_BuildValue("i", 0);
}

